# -*- coding: utf-8 -*-
# @description: 描述符用作装饰器
# @File  : v2.py
# @Time  :2019-09-24 11:44:50
# @Author:selfeasy
# @Email :gm_selfeasy@163.com
from functools import update_wrapper
class description(object):
    def __init__(self,func):
        update_wrapper(self,func)
        self.func=func
        
    def __set__(self,instance,value):#self表示描述符实例，即item.weight，instance是托管实例，即item的实例,value表示对描述符赋值
        instance.__dict__[self.func]=value
        
    def __get__(self,instance,cls):
        if instance is None:
            return self
        key,dicts=self.func.__name__,instance.__dict__
        if key not in dicts:
            self.func(instance)

        return dicts[key]

class test_des(object):
    def __init__(self):
        print(self.boy)

    def info(self):
        self.__dict__['fxx']=123
        self.__dict__['sex']=12222
        self.__dict__['girl']='xxza'
        return 

    @description
    def fxx(self):
        self.info()

    @description
    def sex(self):
        self.info()

    @description
    def girl(self):
        self.info()

    @description
    def boy(self):
        self.__dict__['boy'] = 'sasa'
        
test=test_des()
print(test.__dict__)
print (test.sex) #等价于print(type(test).__dict__['sex'].__get__(test, type(test)))
print (test.girl)
print (test.fxx)
print (test.boy)




